package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//医院设置接口
@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet") //请求映射
//@CrossOrigin //解决跨域问题
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //模拟登录
    @ApiOperation(value = "模拟登录")
    @PostMapping("login")
    //{"code":20000,"data":{"token":"admin-token"}}
    public R login(){
        return R.ok().data("token","admin-token");
    }

    //模拟获取用户信息
    @ApiOperation(value = "模拟获取用户信息")
    @GetMapping("info")
    //{"code":20000,"data":{"roles":["admin"],
//"introduction":"I am a super administrator",
//"avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
//"name":"Super Admin"}}
    public R info(){
        Map<String,Object> map = new HashMap<>();
        map.put("roles","admin");
        map.put("introduction","I am a super administrator");
//        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"); //avatar 头像
        map.put("avatar","http://pic.imeitou.com/uploads/allimg/210107/3-21010G33035.jpg"); //可达鸭头像
        map.put("name","王小虎"); // 超级管理员
        return R.ok().data(map);
    }

    //查询所有
    @ApiOperation(value = "查询所有")
    @GetMapping("findAll")
    public R findAll(){
        /*try {
            int i = 1/0;
        } catch (Exception e) {
            throw new YyghException(20001,"自定义异常处理");
        }*/
//        int i = 1/0;

        //空指针   统一异常处理
//        String str = null;
//        System.out.println("str.length() = " + str.length());

        //角标越界
        try {
            ArrayList<Object> list1 = new ArrayList<>();
            System.out.println("list1.get(0) = " + list1.get(0));
        } catch (Exception e) {
            throw new YyghException(20088,"角标越界异常处理");
        }
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list",list);
    }

    //跟据id删除
    @ApiOperation(value = "跟据id删除")
    @DeleteMapping("{id}")
    public R delById(@PathVariable Long id){
        boolean remove = hospitalSetService.removeById(id);
        return R.ok();
    }

    //分页查询医院设置
    @ApiOperation(value = "分页查询医院设置")
    @GetMapping("pageQuery/{page}/{limit}")
    public R pageList(@PathVariable Long page,@PathVariable Long limit){
        Page<HospitalSet> pageParam = new Page<>(page, limit);
        Page<HospitalSet> pageModle = hospitalSetService.page(pageParam);
        List<HospitalSet> records = pageModle.getRecords();
        long total = pageModle.getTotal();
        return R.ok().data("records",records).data("total",total);

    }

    //带条件带分页查询   医院设置
    @ApiOperation(value = "带条件带分页查询医院设置")
    @PostMapping("pageQuery/{page}/{limit}")
    public R pageQuery(@PathVariable Long page, @PathVariable Long limit,
                       @RequestBody HospitalSetQueryVo hospitalSetQueryVo){
        //1、取出查询条件
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        //2、验空，拼写查询条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hosname);
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hoscode);
        }
        //3、进行分页查询
        Page<HospitalSet> pageParam = new Page<>(page, limit);
        System.out.println("当前页 = " + pageParam.getCurrent());
        Page<HospitalSet> pageModle = hospitalSetService.page(pageParam,wrapper);
        //4、获取结果封装返回
        List<HospitalSet> records = pageModle.getRecords();
        long total = pageModle.getTotal();
        return R.ok().data("records",records).data("total",total);

    }


    @ApiOperation(value = "新增医院设置")
    @PostMapping("save")
    public R save(@RequestBody HospitalSet hospitalSet) {

        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据id查询医院设置
    @ApiOperation(value = "根据id查询医院设置")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("hospitalSet",hospitalSet);
    }


    @ApiOperation(value = "修改医院设置")
    @PostMapping("update")
    public R update(@RequestBody HospitalSet hospitalSet){
        boolean update = hospitalSetService.updateById(hospitalSet);
        if(update){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public R batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        boolean remove = hospitalSetService.removeByIds(idList);
        if(remove){
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //1先查询
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //2更新状态
        hospitalSet.setStatus(status);
        //3调方法更新表数据
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }


}
